//
//  StrategyDetailViewController.m
//  WisdomZhuHai
//
//  Created by Admin on 14-11-4.
//  Copyright (c) 2014年 wyd. All rights reserved.
//

#import "StrategyDetailViewController.h"
#import "HotRateView.h"
#import "TransforController.h"
#import "BottomBarViewController.h"

@interface StrategyDetailViewController ()

@property (nonatomic,strong) NSMutableArray* strategyByDays;//按天数重新拆分的攻略数组，每一项为当天的攻略数组

@property (nonatomic,strong) UIScrollView *scrollView;


@property(nonatomic, strong) BottomBarViewController *bottomBarController;

@end

const static CGFloat PHOTO_HEIGHT = 160;
const static CGFloat OFFSET_X = 10;
const static CGFloat INTERVAL_Y = 15;
const static CGFloat TITLE_HEIGHT = 60.5;
const static CGFloat TIMELINE_WIDTH = 15;
const static CGFloat TIMELINE_OFFSET_X = 16;
const static CGFloat DAY_HEIGHT = 30;
const static CGFloat CONTENT_TITLE_HEIGHT = 28;
const static CGFloat CONTENT_MARGIN_X = 10;
const static CGFloat CONTENT_MARGIN_Y = 8;
const static CGSize DOT_SIZE = {18,12};
const static CGFloat TIANGLE_OFFSET = 14;

@implementation StrategyDetailViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self divideStrategyByDays];
    self.title = @"详细攻略";
    [self initToolbar];
    self.view.backgroundColor = [Common colorWithRGB:0xdbdcdc];
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width, self.view.bounds.size.height - HEIGHT_BOTTOM_BAR)];
    self.scrollView.alwaysBounceVertical = YES;
    [self.view addSubview:self.scrollView];
    
    NSLog(@"%s || %@", __FUNCTION__, self.strategyInfo);
    NSLog(@"%s || %@", __FUNCTION__, self.detailInfo);
    
    CGFloat height = 0;
    
    NSString *photoPath = [Common valueForKey:P_PHOTO dic:self.strategyInfo];
    if(photoPath == nil){
        photoPath = [Common
                     valueForKey:@"cover" dic:self.strategyInfo];
    }
    UIImageView *photoView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.bounds.size.width, PHOTO_HEIGHT)];
    photoView.contentMode = UIViewContentModeScaleAspectFill;
    photoView.clipsToBounds = YES;
    [photoView setImageWithURL:[Common getFullDownloadPathByRelativePath:photoPath] placeholderImage:[Common getPlaceholderImage:ePIT_StorePhoto]];
    [self.scrollView addSubview:photoView];
    height += photoView.bounds.size.height;
    
    UIView *titleView = [self createTitleView];
    CGRect frame = titleView.frame;
    frame.origin.y = height + INTERVAL_Y;
    titleView.frame = frame;
    [self.scrollView addSubview:titleView];
    height += INTERVAL_Y + titleView.bounds.size.height;
    
    for (int i=0; i<self.strategyByDays.count; i++) {
        NSArray* array = self.strategyByDays[i];
        
        UIView *timeline = [self createTimeLine:height+1 length:INTERVAL_Y+TIANGLE_OFFSET-DOT_SIZE.height/2];
        [self.scrollView addSubview:timeline];
        
        UIView *dot = [self createDotView:height+INTERVAL_Y+TIANGLE_OFFSET-DOT_SIZE.height/2 type:0];
        [self.scrollView addSubview:dot];
        
        UIView *dayView = [self createDayView:i];
        frame = dayView.frame;
        frame.origin.y = height + INTERVAL_Y;
        dayView.frame = frame;
        [self.scrollView addSubview:dayView];
        height += INTERVAL_Y + dayView.bounds.size.height;
        
        for (int j=0; j<array.count; j++) {
            CGFloat y = height+1;
            CGFloat length = INTERVAL_Y+TIANGLE_OFFSET-DOT_SIZE.height/2;
            if (j == 0) {
                CGFloat offset = DAY_HEIGHT - TIANGLE_OFFSET - DOT_SIZE.height/2;
                y -= offset;
                length += offset;
            }
            UIView *timeline = [self createTimeLine:y length:length];
            [self.scrollView addSubview:timeline];
            
            UIView *dot = [self createDotView:height+INTERVAL_Y+TIANGLE_OFFSET-DOT_SIZE.height/2 type:1];
            [self.scrollView addSubview:dot];
            
            NSDictionary* dic = array[j];
            
            UIView *contentView = [self createContentView:dic];
            frame = contentView.frame;
            frame.origin.y = height + INTERVAL_Y;
            contentView.frame = frame;
            [self.scrollView addSubview:contentView];
            height += INTERVAL_Y + contentView.bounds.size.height;
        }
    }
    
    self.scrollView.contentSize = CGSizeMake(0, height+INTERVAL_Y);
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)initToolbar{
    self.bottomBarController = [[BottomBarViewController alloc]initWithFrame:CGRectMake(0, self.view.bounds.size.height - HEIGHT_BOTTOM_BAR, 320, 45)];
    NSLog(@"%s || height of view:%f and screen:%f and bottomBar's position: %f, %f", __FUNCTION__, self.view.bounds.size.height, [UIScreen mainScreen].bounds.size.height, self.bottomBarController.view.frame.origin.x, self.bottomBarController.view.frame.origin.y);
    self.bottomBarController.bottomBarNavigationController = self.navigationController;
    [self.view addSubview:self.bottomBarController.view];
    if([[Common valueForKey:@"id" dic:self.strategyInfo] length] > 0){
        [self.bottomBarController setEnabledWithData:[NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"STRATEGY",[Common valueForKey:@"id" dic:self.strategyInfo], nil] forKeys:[NSArray arrayWithObjects:@"type",@"id", nil]]];
    }
}

#pragma mark - Private
- (void)divideStrategyByDays
{
    NSArray *list = [Common valueForKey:P_LIST dic:self.detailInfo];
    if (list.count == 0) {
        return;
    }
    self.strategyByDays = [NSMutableArray array];
    for (NSDictionary* dic in list) {
        int day = [[Common valueForKey:P_DAY dic:dic] intValue];
        NSMutableArray *array = nil;
        if (day <= self.strategyByDays.count) {
            array = self.strategyByDays[day-1];
        }
        if (array == nil) {
            array = [NSMutableArray array];
            self.strategyByDays[day-1] = array;
        }
        [array addObject:dic];
    }
}

- (UIView*)createTitleView
{
    NSString *name = [Common valueForKey:P_NAME dic:self.strategyInfo];
    //NSString *source = [Common valueForKey:P_SOURCE dic:self.strategyInfo];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(OFFSET_X, 0, self.scrollView.bounds.size.width - OFFSET_X*2, TITLE_HEIGHT)];
    view.backgroundColor = [Common colorWithRGB:0xf7f7f7];
    view.layer.cornerRadius = 3.0;
    
    UILabel *lbTitle = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, view.bounds.size.width - 40, (TITLE_HEIGHT-0.5)/2)];
    lbTitle.text = [NSString stringWithFormat:@"旅游攻略：%@",name];
    lbTitle.backgroundColor = [UIColor clearColor];
    lbTitle.font = [UIFont systemFontOfSize:14];
    [view addSubview:lbTitle];
    
    UILabel *lbSource = [[UILabel alloc] initWithFrame:CGRectMake(20, (TITLE_HEIGHT-0.5)/2+0.5, view.bounds.size.width - 40, (TITLE_HEIGHT-0.5)/2)];
//    lbSource.text = [NSString stringWithFormat:@"信息来源：%@", CHECKSTRING(source)];
    lbSource.text = @"信息来源：OK珠海";
    lbSource.backgroundColor = [UIColor clearColor];
    lbSource.font = [UIFont systemFontOfSize:14];
    [view addSubview:lbSource];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(20, (TITLE_HEIGHT-0.5)/2, view.bounds.size.width - 40, 0.5)];
    line.backgroundColor = [UIColor darkGrayColor];
    [view addSubview:line];
    
    return view;
}

- (UIView*)createDayView:(int)day
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(OFFSET_X+TIMELINE_WIDTH, 0, self.scrollView.bounds.size.width - OFFSET_X*2-TIMELINE_WIDTH, DAY_HEIGHT)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, view.bounds.size.width - 16, DAY_HEIGHT)];
    lb.backgroundColor = [UIColor clearColor];
    lb.text = [NSString stringWithFormat:@"第%d天", day+1];
    lb.font = [UIFont systemFontOfSize:15];
    [view addSubview:lb];
    
    return view;
}

- (UIView*)createContentView:(NSDictionary*)dic
{
    NSString *name = [Common valueForKey:P_SITE_NAME dic:dic];
    NSString *intro = [Common valueForKey:P_INTRO dic:dic];
    int star = [[Common valueForKey:P_STAR dic:dic] intValue];
    
    CGFloat width = self.scrollView.bounds.size.width - OFFSET_X*2-TIMELINE_WIDTH;
    CGSize size = [Common textSizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(width - CONTENT_MARGIN_X*2, MAXFLOAT) string:intro];
    CGFloat height = CONTENT_TITLE_HEIGHT + size.height + CONTENT_MARGIN_Y*2;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(OFFSET_X+TIMELINE_WIDTH, 0, width, height)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *lbTitle = [[UILabel alloc] initWithFrame:CGRectMake(CONTENT_MARGIN_X, 4, 200, CONTENT_TITLE_HEIGHT-4)];
    lbTitle.backgroundColor = [UIColor clearColor];
    lbTitle.font = [UIFont systemFontOfSize:14];
    lbTitle.text = name;
    [view addSubview:lbTitle];
    
    HotRateView *hotView = [[HotRateView alloc] initWithHotRate:star];
    CGRect frame = hotView.frame;
    frame.origin.x = width - CONTENT_MARGIN_X - frame.size.width;
    frame.origin.y = (CONTENT_TITLE_HEIGHT - 4 - frame.size.height)/2 + 4;
    hotView.frame = frame;
    [view addSubview:hotView];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(CONTENT_MARGIN_X, CONTENT_TITLE_HEIGHT, width - CONTENT_MARGIN_X*2, 0.5)];
    line.backgroundColor = [UIColor darkGrayColor];
    [view addSubview:line];
    
    UILabel *lbContent = [[UILabel alloc] initWithFrame:CGRectMake(CONTENT_MARGIN_X, CONTENT_TITLE_HEIGHT+0.5, width - CONTENT_MARGIN_X*2, height - CONTENT_TITLE_HEIGHT)];
    lbContent.numberOfLines = 0;
    lbContent.text = intro;
    lbContent.backgroundColor = [UIColor clearColor];
    lbContent.font = [UIFont systemFontOfSize:13];
    [view addSubview:lbContent];
    
    return view;
}

- (UIView*)createTimeLine:(CGFloat)y length:(CGFloat)length
{
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(TIMELINE_OFFSET_X, y, 0.5, length)];
    line.backgroundColor = [UIColor darkGrayColor];
    return line;
}

- (UIView*)createDotView:(CGFloat)y type:(int)type
{
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(OFFSET_X+TIMELINE_WIDTH-DOT_SIZE.width, y+1, DOT_SIZE.width, DOT_SIZE.height)];
    imgView.contentMode = UIViewContentModeCenter;
    imgView.backgroundColor = [UIColor clearColor];
    if (type == 0) {
        imgView.image = [UIImage imageNamed:@"strategy/strategy_timeline_org.png"];
    }
    else if (type == 1) {
        imgView.image = [UIImage imageNamed:@"strategy/strategy_timeline_grey.png"];
    }
    return imgView;
}

@end
